#include "Basic/StringBuilder.h"
#include "GxString.h"

namespace GxLibBasic
{
	StringBuilder::StringBuilder(int len)
	{
		FLen = len;
		FStr = new wchar_t[len];
		memset(FStr,0, len*sizeof(wchar_t));
		FPtr = 0;
		FBufInc = 0x100000;
	}

	StringBuilder::~StringBuilder()
	{
		if (FStr)
			delete [] FStr;
	}

	void StringBuilder::Alloc(int len)
	{
		if (len>FLen)
		{
			wchar_t * nstr = new wchar_t[len];
			memset(nstr,0,len*sizeof(wchar_t));
			memcpy(nstr,FStr,FPtr * sizeof(wchar_t));
			delete [] FStr;
			FStr = nstr;
		}
	}

	int StringBuilder::Count()
	{
		return FPtr;
	}

	void StringBuilder::SetBufferIncrement(int _inc)
	{
		FBufInc = _inc;
	}

	void StringBuilder::WriteChar(wchar_t ch)
	{
		if (FPtr + 1 > FLen-1)
			Alloc(FLen + FBufInc);
		FStr[FPtr] = ch;
		FPtr ++;
	}

	void StringBuilder::WriteString(const wchar_t *str, int len)
	{
		if (FPtr + len > FLen-1)
			Alloc(FLen + FBufInc);
		memcpy(FStr + FPtr, str,len * sizeof(wchar_t));
		FPtr += len;
	}

	void StringBuilder::WriteString(String str)
	{
		WriteString(str.GetStrPtr(), str.GetLength());
	}

	void StringBuilder::WriteString(const wchar_t * str)
	{
		WriteString(str, wcslen(str));
	}


	void StringBuilder::Clear()
	{
		FPtr = 0;
		memset(FStr,0, FLen*sizeof(wchar_t));
	}

	wchar_t* StringBuilder::GetStrPtr()
	{
		return FStr;
	}
}